﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Liang.DataBase.Model.Entity;
using Liang.Util.Rest;

namespace RestServer
{
    public class InitRest
    {
        /// <summary>
        /// rest登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static string loginIn(String name, String pwd)
        {
            string result = "200";
            Json json = HttpClients.doGetMethodToObj<Json>(String.Format("{0}{1}{2}{3}", Liang.Model.Const.LOGIN_SRC, Liang.Model.Const.LOGINJSON, "/" + name, "/" + pwd));
            if (json == null || json.remsg == null)
                result = "400";
            if (!"200".Equals(json.remsg.coding) || !"200.1".Equals(json.remsg.coding))
                result = "200";
            return result;
        }


        /// <summary>
        /// 注册信息
        /// </summary>
        /// <param name="organId"></param>
        /// <returns></returns>
        public static Registers registers(sys_config c)
        {
            Json json = HttpClients.doGetMethodToObj<Json>(Liang.Model.Const.REGISTERS + "/" + c.organId + "/" + c.name + "/" + c.phone);
            Registers o = new Registers();
            o.id = json.registers.id;
            o.verifcode = json.registers.verifcode;
            o.isStop = json.registers.isStop;
            return o;
        }

        ///// <summary>
        ///// 机构信息
        ///// </summary>
        ///// <returns></returns>
        //public static List<organ> organList()
        //{
        //    List<Json> jsons = HttpClients.doGetMethodToObjs<Json>(Const.ORGANSLIST);
        //    List<organ> result = new List<organ>();
        //    for (int i = 0; i < jsons.Count; i++)
        //    {
        //        organ o = new organ();
        //        o.code = jsons[i].organ.code;
        //        o.name = jsons[i].organ.name;
        //        result.Add(o);
        //    }
        //    return result;
        //}

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string getRegsiter(sys_config c)
        {
            string result = "200";
            Json json = HttpClients.doGetMethodToObj<Json>(Liang.Model.Const.REGISTER + "/" + c.organId + "/" + c.name + "/" + c.phone + "/" + c.user + "/" + c.copyright);
            if (json == null || json.remsg == null)
                result = "400";
            else
                result = json.remsg.coding;
            return result;
        }

        /// <summary>
        /// 注册审核验证
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool regValidate(sys_config c)
        {
            bool result = true;
            Json json = HttpClients.doGetMethodToObj<Json>(Liang.Model.Const.REGISTERVALI + "/" + c.organId + "/" + c.name + "/" + c.phone + "/" + c.user);
            if (json.remsg != null)
            {
                if (!"200".Equals(json.remsg.coding))
                    result = false;
            }
            else
                result = false;
            return result;
        }

        ///// <summary>
        ///// 机构信息
        ///// </summary>
        ///// <returns></returns>
        //public static organ getOrgan(string organId)
        //{
        //    Json json = HttpClients.doGetMethodToObj<Json>(Const.ORGANS + "/" + organId );
        //    organ o = new organ();
        //    o.code = json.organ.code;
        //    o.name = json.organ.name;
        //    o.opr_time = DateTime.Now;
        //    o.terr_code = "420000";
        //    return o;

        //}

        /// <summary>
        /// 用户信息
        /// </summary>
        /// <param name="organId"></param>
        /// <returns></returns>
        public static List<sys_user> getUsers(string organId, string verifcode)
        {
            List<Json> jsons = HttpClients.doGetMethodToObjs<Json>(Liang.Model.Const.USER + "/" + organId + "/" + verifcode);
            List<sys_user> result = new List<sys_user>();
            for (int i = 0; i < jsons.Count; i++)
            {
                sys_user o = new sys_user();
                o.userId = jsons[i].user.userId;
                o.name = jsons[i].user.name;
                o.password = jsons[i].user.password;
                o.createTime = DateTime.Now;
                //o.roles.userGroupId = "普通权限";
                result.Add(o);
            }
            return result;
        }

        ///// <summary>
        ///// 企业信息
        ///// </summary>
        ///// <returns></returns>
        //public static List<organ> getEnterpries(string verifcode)
        //{
        //    List<Json> jsons = HttpClients.doGetMethodToObjs<Json>(Const.ENTERPRISE + "/" + verifcode);
        //    List<organ> result = new List<organ>();
        //    for (int i = 0; i < jsons.Count; i++)
        //    {
        //        organ o = new organ();
        //        o.code = jsons[i].organ.code;
        //        o.name = jsons[i].organ.name;
        //        o.org_flag = 4;
        //        o.opr_time = DateTime.Now;
        //        result.Add(o);
        //    }
        //    return result;
        //}
    }
}
